import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { usePermissStore } from "../store/permiss";
import Home from "../views/home.vue";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/business/latestData",
  },
  {
    path: "/",
    name: "Home",
    component: Home,
    children: [
      {
        path: "/business/latestData",
        name: "latestData",
        meta: {
          title: "最新数据",
        },
        component: () => import("../views/business/latestData/index.vue"),
      },
      {
        path: "/business/lineAnalyze",
        name: "lineAnalyze",
        meta: {
          title: "线损分析",
        },
        component: () => import("../views/business/lineAnalyze/index.vue"),
      },
      {
        path: "/business/zoneManage",
        name: "zoneManage",
        meta: {
          title: "台区管理",
        },
        component: () => import("../views/business/zoneManage/index.vue"),
      },
      // {
      //     path: "/business/experimentalManagement",
      //     name: "experimentalManagement",
      //     meta: {
      //         title: '实验管理',
      //     },
      //     component: () => import ("../views/business/experimentalManagement/index.vue")
      // },
      // {
      //     path: "/business/deviceManagement",
      //     name: "deviceManagement",
      //     meta: {
      //         title: '终端设备实时数据',
      //     },
      //     component: () => import ("../views/business/deviceManagement/index.vue")
      // },
      {
        path: "/business/lineDataManagement",
        name: "lineDataManagement",
        meta: {
          title: "线损数据管理",
        },
        component: () =>
          import("../views/business/lineDataManagement/index.vue"),
      },
      {
        path: "/business/lineDataManagementDetail",
        name: "lineDataManagementDetail",
        meta: {
          title: "线损数据管理折线图",
        },
        component: () =>
          import("../views/business/lineDataManagementDetail/index.vue"),
      },
      {
        path: "/system/org",
        name: "org",
        meta: {
          title: "区域管理",
        },
        component: () => import("../views/system/org/index.vue"),
      },
      {
        path: "/system/user",
        name: "user",
        meta: {
          title: "用户管理",
        },
        component: () => import("../views/system/user/index.vue"),
      },
      {
        path: "/system/role",
        name: "role",
        meta: {
          title: "角色管理",
        },
        component: () => import("../views/system/role/index.vue"),
      },
      {
        path: "/system/configure",
        name: "configure",
        meta: {
          title: "配置关系数据",
        },
        component: () => import("../views/system/configureData/index.vue"),
      },
      {
        path: "/system/configUser",
        name: "configUser",
        meta: {
          title: "配置数据",
        },
        component: () => import("../views/system/configUserData/index.vue"),
      },

      {
        path: "/tool/import",
        name: "tollImport",
        meta: {
          title: "设备基础数据导入",
        },
        component: () => import("../views/tool/import.vue"),
      },

      {
        path: "/tool/correct",
        name: "toolCorrect",
        meta: {
          title: "设备校准",
        },
        component: () => import("../views/tool/correct.vue"),
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: "登录",
    },
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/login.vue"),
  },
  {
    path: "/403",
    name: "403",
    meta: {
      title: "没有权限",
    },
    component: () => import(/* webpackChunkName: "403" */ "../views/403.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title}`;
  const role = sessionStorage.getItem("ms_username");
  const permiss = usePermissStore();
  if (!role && to.path !== "/login") {
    next("/login");
  } else if (to.meta.permiss && !permiss.key.includes(to.meta.permiss)) {
    // 如果没有权限，则进入403
    next("/403");
  } else {
    next();
  }
});

export default router;
